#pragma once

class dynamic_bitset
{
	static const size_t ItemSize = sizeof(int) *8;
public:
	class BitHolder
	{
	public:
		BitHolder(int& val, int idx)
			:_val(val),_idx(idx)
		{
		}

		void operator = (bool set)
		{
			if(set)
			{
				_val |= 1<<_idx;
			}
			else
			{
				_val &= ~(1<<_idx);
			}
		}

		operator bool() const 
		{ 
			return (_val & (1<<_idx)) != 0; 
		}
	private:
		int&	_val;
		int		_idx;
	};

	dynamic_bitset(size_t size = 0)
		:_size(size),_array((size +ItemSize -1)/ItemSize)
	{
	}

	void resize(size_t size)
	{
		_size = size;
		_array.resize((size +ItemSize -1)/ItemSize);
	}

	void reset()
	{
		for(std::vector<int>::iterator it=_array.begin(); it!=_array.end(); it++)
		{
			*it =0;
		}
	}

	size_t size() const
	{
		return _size;
	}

	bool operator[] (size_t idx) const
	{
		return (_array[idx/ItemSize] & (1<<(idx % ItemSize))) != 0;
	}

	BitHolder operator[] (size_t idx)
	{
		return BitHolder(_array[idx/ItemSize], idx % ItemSize);
	}
private:
	size_t				_size;
	std::vector<int>	_array;
};